﻿#include "CCorrectCtrOper.h"
#include "CDevDataBeltRealign.h"

CCorrectCtrOper::CCorrectCtrOper(CDevDataBeltRealign* pDevDataBeltRealign)
{
	m_pDevData = pDevDataBeltRealign;
	m_pModBusOper = new CModBusOper(pDevDataBeltRealign->m_sPlcIpAddress, pDevDataBeltRealign->m_nPlcPort);  //创建modbus操作对象
	m_nCtrCmdCurr = -1;
	m_nErrCount = 0;
}

CCorrectCtrOper::~CCorrectCtrOper()
{
	if (m_pModBusOper != nullptr)
	{
		delete m_pModBusOper;
		m_pModBusOper = nullptr;
	}
}

bool CCorrectCtrOper::CtrDevice(SCtrInfo& stCtrInfo)
{
	int nCmd = stCtrInfo.m_nCtrCmd;
	bool bResult = false;
	switch (nCmd)
	{
	case enCSetAuto:
	{
		bResult = SetAuto(stCtrInfo);
	}
	break;
	case enCSetMananl:
	{
		bResult = SetManual(stCtrInfo);
	}
	break;
	case enCMalForeward:
	{
		bResult = MalForeward(stCtrInfo);
	}
	break;
	case enCMalForewardStop:
	{
		bResult = MalForewardStop(stCtrInfo);
	}
	break;
	case enCMalRevesal:
	{
		bResult = MalRevesal(stCtrInfo);
	}
	break;
	case enCMalRevesalStop:
	{
		bResult = MalRevesalStop(stCtrInfo);
	}
	break;
	default:
	{
		bResult = true;
	}
	break;
	}
	m_nCtrCmdCurr = -1;  //清空命令
	return bResult;
}

bool CCorrectCtrOper::GetDevData()
{
	m_pDevData->m_mapDevPoint[enPlcConnectState]->setValue(m_pDevData->m_bPlcConnectStateValue);
	m_pDevData->m_mapDevPoint[enLeftCurrVal]->setValue(m_pDevData->m_dLeftCurrVal);
	m_pDevData->m_mapDevPoint[enRightCurrVal]->setValue(m_pDevData->m_dRightCurrVal);
	m_pDevData->m_mapDevPoint[enCamerState]->setValue(m_pDevData->m_nCamerState);
	m_pDevData->m_mapDevPoint[enAlgState]->setValue(m_pDevData->m_nAlgState);

	int nAddr = m_pDevData->m_mapAddrDataR[1].m_sAddrData.toInt();//取到起始地址
	QVector<int> vecReVal;

	static bool bShowConnectErrorLogOnce = true;//显示一次连接失败日志
	if (m_pModBusOper->ReadValEx(nAddr, 80, vecReVal))
	{
		if (m_bPlcState == false)
		{
			m_bPlcState = true;
		}
		m_pDevData->m_bPlcConnectState = true;//PLC连接状态
		m_pDevData->m_bPlcConnectStateValue = true;//PLC连接状态
		bShowConnectErrorLogOnce = true;
		SetAddrData(vecReVal);
		m_pDevData->m_nRecCamerLeft = m_pDevData->m_mapAddrDataR[enCamerLeft].m_nTagVal;
		m_pDevData->m_mapDevPoint[enRecCamerLeft]->setValue(m_pDevData->m_nRecCamerLeft);
		m_pDevData->m_nRecCamerRight = m_pDevData->m_mapAddrDataR[enCamerRight].m_nTagVal;
		m_pDevData->m_mapDevPoint[enRecCamerRight]->setValue(m_pDevData->m_nRecCamerRight);
		if (m_pDevData->m_mapAddrDataR[enCamerDirBack].m_nTagVal > 30000)
		{
			m_pDevData->m_nRecOffsetValue = m_pDevData->m_mapAddrDataR[enCamerDirBack].m_nTagVal - 65536;
		}
		else
		{
			m_pDevData->m_nRecOffsetValue = m_pDevData->m_mapAddrDataR[enCamerDirBack].m_nTagVal;
		}
		m_pDevData->m_mapDevPoint[enRecOffsetValue]->setValue(m_pDevData->m_nRecOffsetValue);
		m_pDevData->m_nRecRemoteAuto = m_pDevData->m_mapAddrDataR[enRemoteAuto].m_nTagVal;
		m_pDevData->m_mapDevPoint[enRecRemoteAuto]->setValue(m_pDevData->m_nRecRemoteAuto);
		m_pDevData->m_nRecRemoteHand = m_pDevData->m_mapAddrDataR[enRemoteMal].m_nTagVal;
		m_pDevData->m_mapDevPoint[enRecRemoteHand]->setValue(m_pDevData->m_nRecRemoteHand);
		m_pDevData->m_nRecErrorAlarm = m_pDevData->m_mapAddrDataR[enErrorAlarm].m_nTagVal;
		m_pDevData->m_mapDevPoint[enRecErrorAlarm]->setValue(m_pDevData->m_nRecErrorAlarm);
		m_pDevData->m_nRecRemoteCtlFeedback = m_pDevData->m_mapAddrDataR[enRemoteCtlFeedback].m_nTagVal;
		m_pDevData->m_mapDevPoint[enRecRemoteCtlFeedback]->setValue(m_pDevData->m_nRecRemoteCtlFeedback);
		m_pDevData->m_nRecLocalCtlFeedback = m_pDevData->m_mapAddrDataR[enLocalCtlFeedback].m_nTagVal;
		m_pDevData->m_mapDevPoint[enRecLocalCtlFeedback]->setValue(m_pDevData->m_nRecLocalCtlFeedback);
		m_pDevData->m_nRecLocalLeftRun = m_pDevData->m_mapAddrDataR[enLocalLeftRun].m_nTagVal;
		m_pDevData->m_mapDevPoint[enRecLocalLeftRun]->setValue(m_pDevData->m_nRecLocalLeftRun);
		m_pDevData->m_nRecLocalRightRun = m_pDevData->m_mapAddrDataR[enLocalRightRun].m_nTagVal;
		m_pDevData->m_mapDevPoint[enRecLocalRightRun]->setValue(m_pDevData->m_nRecLocalRightRun);
		m_pDevData->m_nRecMotorRunFeedback = m_pDevData->m_mapAddrDataR[enMotorRunFeedback].m_nTagVal;
		m_pDevData->m_mapDevPoint[enRecMotorRunFeedback]->setValue(m_pDevData->m_nRecMotorRunFeedback);
		m_pDevData->m_nRecLeftYVRunFeedback = m_pDevData->m_mapAddrDataR[enLeftYVRunFeedback].m_nTagVal;
		m_pDevData->m_mapDevPoint[enRecLeftYVRunFeedback]->setValue(m_pDevData->m_nRecLeftYVRunFeedback);
		m_pDevData->m_nRecRightYVRunFeedback = m_pDevData->m_mapAddrDataR[enRightYVRunFeedback].m_nTagVal;
		m_pDevData->m_mapDevPoint[enRecRightYVRunFeedback]->setValue(m_pDevData->m_nRecRightYVRunFeedback);
	}
	else
	{
		m_nErrCount++;
		if (m_nErrCount > 20)//断线10秒钟才算掉线
		{
			m_pDevData->m_bPlcConnectState = false;
			m_pDevData->m_bPlcConnectStateValue = false;//PLC连接状态
			m_nErrCount = 0;
			//更新测点状态
			m_pDevData->m_mapDevPoint[enRecCamerLeft]->setStatus(EPointQuality::enTimeOut);
			m_pDevData->m_mapDevPoint[enRecCamerRight]->setStatus(EPointQuality::enTimeOut);
			m_pDevData->m_mapDevPoint[enRecOffsetValue]->setStatus(EPointQuality::enTimeOut);
			m_pDevData->m_mapDevPoint[enRecRemoteAuto]->setStatus(EPointQuality::enTimeOut);
			m_pDevData->m_mapDevPoint[enRecRemoteHand]->setStatus(EPointQuality::enTimeOut);
			m_pDevData->m_mapDevPoint[enRecErrorAlarm]->setStatus(EPointQuality::enTimeOut);
			m_pDevData->m_mapDevPoint[enRecRemoteCtlFeedback]->setStatus(EPointQuality::enTimeOut);
			m_pDevData->m_mapDevPoint[enRecLocalCtlFeedback]->setStatus(EPointQuality::enTimeOut);
			m_pDevData->m_mapDevPoint[enRecLocalLeftRun]->setStatus(EPointQuality::enTimeOut);
			m_pDevData->m_mapDevPoint[enRecLocalRightRun]->setStatus(EPointQuality::enTimeOut);
			m_pDevData->m_mapDevPoint[enRecMotorRunFeedback]->setStatus(EPointQuality::enTimeOut);
			m_pDevData->m_mapDevPoint[enRecLeftYVRunFeedback]->setStatus(EPointQuality::enTimeOut);
			m_pDevData->m_mapDevPoint[enRecRightYVRunFeedback]->setStatus(EPointQuality::enTimeOut);
			m_pDevData->m_mapDevPoint[enCamerState]->setStatus(EPointQuality::enTimeOut);
			m_pDevData->m_mapDevPoint[enAlgState]->setStatus(EPointQuality::enTimeOut);
		}

		if (bShowConnectErrorLogOnce)
		{
			bShowConnectErrorLogOnce = false;
		}
		m_pDevData->m_mapDevPoint[enLeftCurrVal]->setValue(m_pDevData->m_dLeftCurrVal);
		m_pDevData->m_mapDevPoint[enRightCurrVal]->setValue(m_pDevData->m_dRightCurrVal);
		m_pDevData->m_mapDevPoint[enCamerState]->setValue(m_pDevData->m_nCamerState);
		m_pDevData->m_mapDevPoint[enAlgState]->setValue(m_pDevData->m_nAlgState);
		return false;
	}
	return true;
}

bool CCorrectCtrOper::SetAuto(SCtrInfo& stCtrInfo)
{
	int nReVal1 = writeData(enRemoteAuto, 1);
	int nReVal2 = writeData(enRemoteMal, 0);
	if (nReVal1 == enSuccess && nReVal2 == enSuccess)
	{
		return true;
	}
	else
	{
		return false;
	}
}

bool CCorrectCtrOper::SetManual(SCtrInfo& stCtrInfo)
{
	int nReVal1 = writeData(enRemoteAuto, 0);
	int nReVal2 = writeData(enRemoteMal, 1);
	if (nReVal1 == enSuccess && nReVal2 == enSuccess)
	{
		return true;
	}
	else
	{
		return false;
	}
}

bool CCorrectCtrOper::MalForeward(SCtrInfo& stCtrInfo)
{
	int nReVal = writeData(enRemoteMalForeward, 1);
	if (nReVal == enSuccess)
	{
		return true;
	}
	else
	{
		return false;
	}
}

bool CCorrectCtrOper::MalForewardStop(SCtrInfo& stCtrInfo)
{
	int nReVal = writeData(enRemoteMalForeward, 0);
	if (nReVal == enSuccess)
	{
		return true;
	}
	else
	{
		return false;
	}
}

bool CCorrectCtrOper::MalRevesal(SCtrInfo& stCtrInfo)
{
	int nReVal = writeData(enRemoteMalRevesal, 1);
	if (nReVal == enSuccess)
	{
		return true;
	}
	else
	{
		return false;
	}
}

bool CCorrectCtrOper::MalRevesalStop(SCtrInfo& stCtrInfo)
{
	int nReVal = writeData(enRemoteMalRevesal, 0);
	if (nReVal == enSuccess)
	{
		return true;
	}
	else
	{
		return false;
	}
}

void CCorrectCtrOper::SetAddrData(QVector<int>vecReVal)
{
	int nAddrStart = m_pDevData->m_mapAddrDataR[1].m_sAddrData.toInt();
	for (QMap<int, SAddrData>::iterator itor = m_pDevData->m_mapAddrDataR.begin(); itor != m_pDevData->m_mapAddrDataR.end(); itor++)
	{
		SAddrData AddrData = itor.value();
		int nAddr = AddrData.m_sAddrData.toInt();
		int nLength = AddrData.m_nAddrLength;
		int nIndex = nAddr - nAddrStart;
		quint64 nData = 0;
		for (int j = 0; j < nLength; j++)
		{
			nData = nData * 65536 + vecReVal[nIndex + j];
		}
		itor.value().m_nTagVal = nData;
	}
}

int CCorrectCtrOper::writeData(int nAddr, int nData /*= 0*/)
{
	if (m_pModBusOper->WriteVal(m_pDevData->m_mapAddrDataR[nAddr].m_sAddrData.toInt(), nData) == true)
	{
		return enSuccess;
	}
	else
	{
		return enWriteParamFail;
	}
}
